﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Aidbrella.Web.ViewModels;
using AidbrellaWCFProxy;
using Aidbrella.Web.Common;
using AidbrellaWCFProxy.aidBrellaService;
namespace Aidbrella.Web.Controllers
{
    public partial class OrganizationController : BaseController
    {
       private OrganizationViewModel _organizationVM = new OrganizationViewModel();
       private ProjectViewModel _projectVM = new ProjectViewModel();
       List<OrganizationViewModel> models = new List<OrganizationViewModel>();
       List<ProjectViewModel> orgProjects = new List<ProjectViewModel>();

       public OrganizationController()
       {
           models = GetOrganization();
       }
         
        //
        // GET: /Organization/

       public virtual ActionResult Show(int id)
        {
            _organizationVM = models.Where(o => o.OrganizationId == id).FirstOrDefault();
            
            return View(_organizationVM);
        }

       

        [HttpGet]
       public virtual ActionResult Edit(int id)
        {
            _organizationVM = models.Where(o => o.OrganizationId == id).FirstOrDefault();

            return View(_organizationVM);
        }

        [HttpPost]
        public virtual ActionResult Edit(OrganizationViewModel orgVM)
        {
            var org = models.Where(o => o.OrganizationId == orgVM.OrganizationId).FirstOrDefault();

            models.Remove(org);
            models.Add(orgVM);
            return RedirectToAction("Index", new { id = _organizationVM.OrganizationId });
           // return View(_organizationVM);
        }

        /// <summary>
        /// Dummy data provider
        /// </summary>
        /// <param name="orgId"></param>
        /// <returns></returns>
        private List<OrganizationViewModel> GetOrganization()
        {
            
            for (int i = 0; i < 5; i++)
            {
                var model = new OrganizationViewModel
                {
                    OrganizationId = i,
                    Address = "Puntaritie 2 A 13",
                    Description = "A good organization",
                    Name = "UN " + i.ToString(),
                    Website = "http://microsoft.com",
                    Registration = new RegistrationViewModel
                    {
                        Country = "Finland",
                        DateOfRegistration = DateTime.Now,
                        OrganizationId = i,
                        OrganizationType = Common.OrganizationType.NGO,
                        RegNo = i.ToString(),
                    },
                    Projects = GetProjects(i),
                  
                };
                
                model.ProjectLocations = GetProjects(i).SelectMany(o => o.ProjectLocations).ToList();
                models.Add(model);

            }
            return models;
        }


        /// <summary>
        /// List of Organization
        /// </summary>
        /// <returns></returns>
        public ActionResult GetOrganizations()
        {
            //TODO : Filter should be added not to clog the request


            AidBrellaServiceClient client = new AidBrellaServiceClient();
            Dictionary<Organization, Project[]> organizationProjects = new Dictionary<Organization, Project[]>(); 

            var orgmodel = client.GetOrganizations();

            foreach (var item in orgmodel)
            {
                organizationProjects.Add(item, client.GetProjectsOfOrganization(item.Id));             
            }

            List<OrganizationViewModel> organs = new List<OrganizationViewModel>();
            OrganizationViewModel organ;
           

            foreach (var orgpro in organizationProjects)
            {
                int i = 1;
                organ = PopulateProjectsForOrganization(orgpro,client);
                organ.ProjectLocations = GetRandomLocation(organ.Name, i);
                i++;
                organs.Add(organ);
            }


            return View(organs);
        }

        private OrganizationViewModel PopulateProjectsForOrganization(KeyValuePair<Organization, Project[]> orgpro, AidBrellaServiceClient client)
        {
            OrganizationViewModel model = new OrganizationViewModel();
            Organization organization = orgpro.Key;
            model.OrganizationId = organization.Id;
            model.Name = organization.Name;
            model.Projects = new List<ProjectViewModel>();
            ProjectViewModel project;

            foreach (Project item in orgpro.Value)
            {

              // ProjectSite [] sites = client.GetSitesForProject(item.Id);

                project = new ProjectViewModel
                              {
                                  Organization = model,
                                  ProjectId = item.Id,
                                  Title = item.Name,
                                  Description = item.Description,
                                  //ProjectLocations = GetRandomLocation(item)
                              };

                //foreach (var site in sites)
                //{
                    
                //}
               
                model.Projects.Add(project);
            }

          //  model.ProjectLocations = model.Projects.SelectMany(o => o.ProjectLocations).ToList();

            return model;
        }

        private List<ProjectSiteViewModel> GetRandomLocation(string title, int i)
        {
            List<ProjectSiteViewModel> projectSites = new List<ProjectSiteViewModel>();
            
                ProjectSiteViewModel pv = new ProjectSiteViewModel();
                pv.Location = new LocationViewModel { Latitude = -18.77718, Longitude = 46.854321 };
                pv.ProjectStatus = ProjectStatus.Approved;
                pv.Name = title;
                //pv.ProjectId = item.Id;
                //pv.Name = item.Name;

                projectSites.Add(pv);

                ProjectSiteViewModel pv1 = new ProjectSiteViewModel();
                pv1.Location = new LocationViewModel { Latitude = -28.47933, Longitude = 24.67993 };
                pv1.ProjectStatus = ProjectStatus.CompletedWithSuccess;
                //pv1.ProjectId = item.Id;
                //pv1.Name = item.Name;

                projectSites.Add(pv1);

                ProjectSiteViewModel pv2 = new ProjectSiteViewModel();
                pv2.Location = new LocationViewModel { Latitude = 9.08457, Longitude = 8.67425 };
                pv2.ProjectStatus = ProjectStatus.CompletedWithSuccess;
                //pv2.ProjectId = item.Id;
                //pv2.Name = item.Name;

                projectSites.Add(pv2); 

            return projectSites;
        }

      
        private List<ProjectViewModel> GetProjects(int orgId)
        {

            for (int i = orgId; i < 10; i++)
            {
                ProjectSiteViewModel projectSiteViewModel = new ProjectSiteViewModel
               {
                   Location = new LocationViewModel { Latitude =  (15.130262012034756 + i), Longitude = (38.61523437499999 + i) },
                   ProjectId = 1,
                   ProjectStatus = ProjectStatus.OnResearch,
                   Name = "Project Location"
               };

                ProjectSiteViewModel projectSiteViewModel2 = new ProjectSiteViewModel
                {
                    Location = new LocationViewModel { Latitude = (16.130262012034756 + i), Longitude = (45.61523437499999 + i) },
                    ProjectId = 1,
                    ProjectStatus = ProjectStatus.OnResearch,
                    Name = "Another project Location"
                };

                ProjectViewModel prVM = new ProjectViewModel();

                prVM.Description = "Project 1";
                prVM.Organization = _organizationVM;
                prVM.Title = "Enable aidBrella";
                prVM.ProjectLocations = new List<ProjectSiteViewModel> { projectSiteViewModel, projectSiteViewModel2 };
                orgProjects.Add(prVM); 
            }

            return orgProjects;
        }

    }

   
       
    
}
